package axis.dynamic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.axis.AxisEngine;
import org.apache.axis.Chain;
import org.apache.axis.Handler;
import org.apache.axis.SimpleChain;
import org.apache.axis.constants.Style;
import org.apache.axis.constants.Use;

/**
 * base class for soap senders and receivers.
 * 
 * TODO: refactor code that this class has in common with SoapConfiguration.
 */
public abstract class SoapInterface
{
  private List<Handler> _requestHandlers;
  
  private List<Handler> _responseHandlers;
  
  protected Map<String, Object> _properties = new HashMap<String, Object>();
  
  public abstract void setStyle( Style style );

  public abstract void setUse( Use use );

  
  public void setSendMultiRefs( boolean multiRefs )
  {
    setProperty(AxisEngine.PROP_DOMULTIREFS, Boolean.valueOf(multiRefs));
  }
  
  public void setSendXsiTypes( boolean xsiTypes )
  {
    setProperty(AxisEngine.PROP_SEND_XSI, Boolean.valueOf(xsiTypes));
  }
  
  public void setUsePrettyXml( boolean pretty )
  {
    setProperty(AxisEngine.PROP_DISABLE_PRETTY_XML, Boolean.valueOf(! pretty));
  }
  
  /*
  public void setSendXmlDeclaration( boolean sendXmlDeclaration )
  {
    setProperty(AxisEngine.PROP_XML_DECL, Boolean.valueOf(sendXmlDeclaration));
  }
  */
  
  /*
  public void setDotNetSoapEncFix( boolean dotNetSoapEncFix )
  {
    setProperty(AxisEngine.PROP_DOTNET_SOAPENC_FIX, Boolean.valueOf(dotNetSoapEncFix));
  }
  */
  
  protected void setProperty( String name, Object value )
  {
    _properties.put(name, value);
  }

  public void setRequestHandlers( List<Handler> handlers )
  {
    _requestHandlers = handlers;
  }

  public void addRequestHandler( Handler handler )
  {
    if (_requestHandlers == null) _requestHandlers = new ArrayList<Handler>();
    _requestHandlers.add(handler);
  }

  public void setResponseHandlers( List<Handler> handlers )
  {
    _responseHandlers = handlers;
  }

  public void addResponseHandler( Handler handler )
  {
    if (_responseHandlers == null) _responseHandlers = new ArrayList<Handler>();
    _responseHandlers.add(handler);
  }
  
  /**
   * @return may be null
   */
  protected Chain getRequestChain()
  {
    return getChain(_requestHandlers);
  }

  /**
   * @return may be null
   */
  protected Chain getResponseChain()
  {
    return getChain(_responseHandlers);
  }

  /**
   * @param handlers 
   * @return null if given list is null
   */
  private Chain getChain( List<Handler> handlers )
  {
    if (handlers == null) return null;
    
    SimpleChain chain = new SimpleChain();
    for (Handler handler : handlers)
    {
      chain.addHandler(handler);
    }
    return chain;
  }
  
}
